<template>
	<view>
		<uni-section title="工资发放/扣减" type="line" />
		<view style="padding: 0 10px;">
			<uni-forms style="width: 95%; margin: 10rpx auto;"
			ref="baseForm" 
			:rules="rules" 
			labelAlign="right" 
			labelWidth="80"
			:modelValue="salary">
				<uni-forms-item  label="团队:" name="teamId">
					<uni-data-picker 
					placeholder="请选择团队" 
					:localdata="team" 
					@change="getProjectsByCondition"
					v-model="salary.teamId" />
				</uni-forms-item>
				<uni-forms-item label="项目:" required name="project">
					<uni-data-picker
					placeholder="请选择工程" 
					:localdata="project" 
					@change="getEmployee"
					v-model="salary.projectId" />
				</uni-forms-item>
				<uni-forms-item label="员工:" required name="employee">
					<uni-data-checkbox 
					mode="button" 
					v-model="salary.employeeId" 
					:localdata="worker">
					</uni-data-checkbox>
				</uni-forms-item>
				<uni-forms-item label="发放/扣减:" required name="addMinus">
					<uni-data-picker
					placeholder="请选择付款方式" 
					:localdata="addMinus" 
					v-model="salary.addMinus" />
				</uni-forms-item>
				<uni-forms-item label="金额:" required name="pay">
					<uni-easyinput type="text" v-model="salary.pay" placeholder="0.00" />
				</uni-forms-item>
				<uni-forms-item label="付款方式:" required name="payment">
					<uni-data-picker
					placeholder="请选择付款方式" 
					:localdata="payment" 
					v-model="salary.payment" />
				</uni-forms-item>
				<uni-forms-item label="备注:">
					<uni-easyinput type="textarea" v-model="salary.note" placeholder="请输入备注" />
				</uni-forms-item>
			</uni-forms>
			<button type="primary" @click="submit" style="background-color: #007aff;">提交</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				team: [],
				project: [],
				worker: [],
				payment: [
					{
						text:'现金',
						value: 'CASH'
					},
					{
						text:'微信',
						value: 'WEIXIN'
					},
					{
						text:'支付宝',
						value: 'ZFB'
					},
					{
						text:'银行卡',
						value: 'BANK'
					},
				],
				addMinus: [
					{
						text:'发放',
						value: 'ADD'
					},
					{
						text:'扣减',
						value: 'MINUS'
					},
				],
				salary: {
					teamId: '',
					projectId: '',
					employeeId: '',
					pay: 0,
					payment: 'CASH',
					addMinus: 'ADD',
					note: '无',
				},
				rules: {
					project: {
						rules: [{
							required: true,
							errorMessage: '请先选择工程'
						}]
					},
					pay: {
						rules: [{
							required: true,
							errorMessage: '请输入发放金额'
						}]
					},
					payment: {
						rules: [{
							required: true,
							errorMessage: '请选择付款方式'
						}]
					},
					employee: {
						rules: [{
							required: true,
							errorMessage: '请选择员工'
						}]
					},
					addMinus: {
						rules: [{
							required: true,
							errorMessage: '请选择发放还是扣减'
						}]
					}
				},
			}
		},
		onLoad() {
			this.getTeam();
			this.getProjectsByCondition();
			this.getEmployee();
		},
		onReady() {
			// 需要在onReady中设置规则
			this.$refs.baseForm.setRules(this.rules)
		},
		onShareAppMessage(res) {
		    if (res.from === 'button') {// 来自页面内分享按钮
		      console.log(res.target)
		    }
		    return {
		      title: '大家都在用这款小工具管理项目，快来试试吧！！！',
		      path: '/pages/index/index'
		    }
		},
		computed: {
			// newProject() {
			// 	return this.salary.projectId;
			// }
		},
		watch: {
			// newProject(val) {
			// 	this.getEmployee(val);
			// },
			// immediate: true
		},
		methods: {
			async getTeam() {
				var list = await this.$dry.getTeams(true);
				var team = [{
					text: "请选择团队",
					value: ''
				}];
				list.forEach(e => {
					team.push({
						text: e.name,
						value: e.id
					})
				})
				this.team = team;
				
			},
			async getProjectsByCondition(e) {
				var projectList = this.$dry.get("project");
				var teamId = '';
				if(e) {
					teamId = e.detail.value[0].value;
				}
				projectList = await this.getProjects(teamId);
				if(projectList.length != 0) {
					var newList = [{
						text: "请选择项目",
						value: ''
					}]
					projectList.forEach(p => {
						newList.push({
							text: p.name,
							value: p.id,
						});
					});
					this.project = newList;
				}
				else{
					this.project = [];
				}
			},
			getProjects(teamId) {
				var token = this.$dry.getToken();
				var data = [];
				if (token) {
					data = this.$request.run("queryProjectsByCondition",token);
					return new Promise((resolve) => {
						uni.request({
							url: data.url,
							method: data.method,
							header: data.header,
							data: {
								"teamId": teamId,
								"queryAll": false
							},
							success: data => {
								resolve(data.data);
							}
						});
					});
				}
			},
			getEmployee() {
				var token = this.$dry.getToken();
				if(token){
					var data = this.$request.run("listEmployeeByConditions",token);
				    uni.request({
				    	url: data.url,
				    	method: data.method,
				    	header: data.header,
				    	data: {
							"projectId": this.salary.projectId,
							"employeeStatus": 'NORMAL'
						},
				    	success: data => {
				    		var worker = data.data;
							var newItems = [];
							if(worker.length != 0){
								worker.forEach(e => {
									newItems.push({
										text: e.realName == null || e.realName == '' ? e.nickName : e.realName,
										value: e.id
									});
								});
							}
							this.worker= newItems;
				    	},
				    	fail: (data, code) => {
				    		console.log('fail' + JSON.stringify(data));
				    	}
				    })
				}
			},
			submit() {
				this.$refs['baseForm'].validate().then(res => {
					var token = this.$dry.getToken();
					if(token){
						var data = this.$request.run("addSalary", token);
						uni.request({
							url: data.url,
							method: data.method,
							header: data.header,
							data: this.salary,
							success: data => {
								if(data.statusCode === 200) {
									uni.showToast({
										title: "操作成功！",
										duration: 1000
									});
									uni.navigateBack({
										delta: 1
									})
								}
							}
						})
					}
				}).catch(err => {
					console.log('err', err);
				})
			},
		}
	}
</script>

<style>

</style>
